package com.ruoyi.erp.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.erp.domain.SettlePo;
import com.ruoyi.erp.domain.StaffingPo;
import com.ruoyi.erp.service.IStaffingPoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.erp.domain.BillSettle;
import com.ruoyi.erp.service.IBillSettleService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 外派结算Controller
 * 
 * @author sauger
 * @date 2022-03-24
 */
@Controller
@RequestMapping("/erp/settle")
public class BillSettleController extends BaseController
{
    private String prefix = "erp/settle";

    @Autowired
    private IBillSettleService billSettleService;
    @Autowired
    private IStaffingPoService staffingPoService;

    @RequiresPermissions("erp:settle:view")
    @GetMapping()
    public String settle()
    {
        return prefix + "/settle";
    }

    /**
     * 查询外派结算列表
     */
    @RequiresPermissions("erp:settle:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BillSettle billSettle)
    {
        startPage();
        List<BillSettle> list = billSettleService.selectBillSettleList(billSettle,true);
        return getDataTable(list);
    }

    /**
     * 导出外派结算列表
     */
    @RequiresPermissions("erp:settle:export")
    @Log(title = "外派结算", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BillSettle billSettle)
    {
        List<BillSettle> list = billSettleService.selectBillSettleList(billSettle);
        ExcelUtil<BillSettle> util = new ExcelUtil<BillSettle>(BillSettle.class);
        return util.exportExcel(list, "外派结算数据");
    }

    /**
     * 新增外派结算
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存外派结算
     */
    @RequiresPermissions("erp:settle:add")
    @Log(title = "外派结算", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BillSettle billSettle)
    {
        return toAjax(billSettleService.insertBillSettle(billSettle));
    }

    /**
     * 修改外派结算
     */
    @RequiresPermissions("erp:settle:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        BillSettle billSettle = billSettleService.selectBillSettleById(id);
        billSettleService.loadDetails(billSettle);
        StaffingPo search = new StaffingPo();
        search.setCustomerId(billSettle.getCustomerId());
        search.setIsFinish("N");
        List<StaffingPo> staffingPos = staffingPoService.selectStaffingPoList(search);
        staffingPos.stream().filter(staffingPo -> !billSettle.getSettlePos().stream().map(m -> m.getId()).collect(Collectors.toList()).contains(staffingPo.getId())).forEach(item->{
            SettlePo settlePo = new SettlePo();
            settlePo.setResourceId(item.getId());
            settlePo.setResourceType("po");
            settlePo.setId(item.getId());
            settlePo.setBilledAmount(item.getBilledAmount());
            settlePo.setCustomerId(item.getCustomerId());
            settlePo.setProjectId(item.getProjectId());
            settlePo.setProjectName(item.getProjectName());
            settlePo.setOrderAmount(item.getOrderAmount());
            settlePo.setTitle(item.getTitle());
            settlePo.setStartDate(item.getStartDate());
            settlePo.setEndDate(item.getEndDate());
            settlePo.setIsFinish(item.getIsFinish());
            settlePo.setAmount(BigDecimal.ZERO);
            billSettle.getSettlePos().add(settlePo);
        });

        mmap.put("billSettle", billSettle);
        return prefix + "/edit";
    }

    /**
     * 修改保存外派结算
     */
    @RequiresPermissions("erp:settle:edit")
    @Log(title = "外派结算", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BillSettle billSettle)
    {
        return toAjax(billSettleService.updateBillSettle(billSettle));
    }

    /**
     * 删除外派结算
     */
    @RequiresPermissions("erp:settle:remove")
    @Log(title = "外派结算", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(Integer ids)
    {
        return toAjax(billSettleService.deleteBillSettleById(ids));
    }
}
